package com.pioneer.gui.invoice.controller;

import com.pioneer.gui.invoice.view.CustomerPanel;
import com.pioneer.gui.invoice.view.InvoicePanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ComboBoxModel;

public class CustomerTextFieldtoInvoiceListAdapter
extends KeyAdapter 
{	
	private InvoicePanel _invoicePanel;
	private CustomerPanel _customerPanel;
	
	public CustomerTextFieldtoInvoiceListAdapter(InvoicePanel invoicePanel, CustomerPanel customerPanel)
	{
		this._invoicePanel=invoicePanel;
		this._customerPanel=customerPanel;
	}


	public void keyReleased(KeyEvent e)
		{
			DropDownList customerDropDown=(DropDownList)InvoicePanel.this.customerDropDown.getModel();
			Vector list=customerDropDown.getDropDownValues();
			int i=1;
			boolean found=false;
			while (!found&&i<list.size())
			{
				String curVectorValue=((Object[])list.elementAt(i))[1].toString();
				String curFieldValue=InvoicePanel.this.customerTextField.getText();
				if (curVectorValue.length()>=curFieldValue.length())
				{
					if (curFieldValue.equalsIgnoreCase(curVectorValue.substring(0,curFieldValue.length())))
					{
						found=true;
						//System.out.println("Found match at "+curVectorValue);
						if (curFieldValue.length()>0)
							InvoicePanel.this.customerDropDown.setSelectedIndex(i--);
						else
							InvoicePanel.this.customerDropDown.setSelectedIndex(0);
						InvoicePanel.this.customerDropDown.revalidate();
						InvoicePanel.this.customerDropDown.repaint();
					}
				}
				i++;
			}			
		}
	}